#!/usr/bin/php5 -q
<?php
    
    $ZIP = "/usr/bin/zip -r ";

    $REPODIR = "pack/Installer";
    $CATEGORY = "DevImpress";
    $VERSION = time();
    
    //
    // Make package
    //
    $d = dir(".");
    while (false !== ($e = $d->read())) {
	if(is_dir($e) && ".app" === substr($e, -4)) {
	    $app_name = substr($e, 0, strlen($e) - 4);	    
	}
    }
    $d->close();
    
    if(!isset($app_name)) die("Can't find application folder!");
    
    $arc_name = $app_name . "-" . $VERSION . ".zip";
    
    // its simpler than PHP-zip
    system($ZIP . $REPODIR . "/zips/" . $arc_name . " " . 
	$app_name . ".app -x " . $app_name . ".app/.svn\\*"); 
    
    //
    // Create description
    //
    $a = array();
    
    $a['bundle'] = "com.devimpress.".strtolower($app_name);
    $a['appname'] = $app_name;
    $a['location'] = "http://devimpress.com/i/zips/" . $arc_name;
    $a['version'] = $VERSION; 
    $a['size'] = filesize($REPODIR . "/zips/" . $arc_name);
    $a['description'] = "Version " . $VERSION . " of " . $app_name; 
    
    $f = explode("@", implode("", file("pack/_default.plist")));
    $r = fopen($REPODIR . "/plists/" . $CATEGORY . "/" . $app_name . ".plist", "w+");
    foreach($f as $fi) {
	if(isset($a[$fi])) fputs($r, $a[$fi]);
	else fputs($r, $fi);
    }
    fclose($r);
    
?>
